package com.chris.cc.app.service.impl;

import com.chris.cc.app.mapper.MenuMapper;
import com.chris.cc.app.model.dto.MenuDto;
import com.chris.cc.app.model.entity.Menu;
import com.chris.cc.app.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this tree of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this tree of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/8/28 12:12
 **/
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 显示菜单
     *
     * @param appName
     */
    @Override
    public void showMenu(String appName) {
        Example e = new Example(Menu.class);
        e.createCriteria()
                .andEqualTo("appName", appName)
                .andEqualTo("visible", false);
        Menu t = new Menu();
        t.setVisible(true);
        menuMapper.updateByExampleSelective(t, e);
    }

    /**
     * 隐藏菜单
     *
     * @param appName
     */
    @Override
    public void hideMenu(String appName) {
        Example e = new Example(Menu.class);
        e.createCriteria()
                .andEqualTo("appName", appName)
                .andEqualTo("visible", true);
        Menu t = new Menu();
        t.setVisible(false);
        menuMapper.updateByExampleSelective(t, e);
    }

    /**
     * 列出菜单项
     *
     * @param parentId 根节点的parentId=0
     * @param visible  可见且可用
     * @return
     */
    @Override
    public List<Menu> listMenu(Long parentId, Boolean visible) {
        Example e = new Example(Menu.class);
        e.createCriteria()
                .andEqualTo("parentId", parentId);
        if (visible) {
            e.and().andEqualTo("visible", true);
        }
        e.orderBy("order").asc();
        return menuMapper.selectByExample(e);
    }

    @Override
    public void add(MenuDto menu) {
        Menu m = new Menu();
        m.setVisible(true);
        m.setAppName(menu.getAppName());
        m.setComponent(menu.getComponent());
        m.setName(menu.getName());
        m.setOrder(menu.getOrder());
        m.setParentId(menu.getParentId());
        m.setPermissions(menu.getPermissions());
        m.setType(menu.getType());
        m.setPath(menu.getPath());
        m.setCreateDate(new Date());
        menuMapper.insert(m);
    }

    @Override
    public void update(MenuDto menu) {
        Menu m = menuMapper.selectByPrimaryKey(menu.getId());
        if (m == null) {
            return;
        }
        m.setAppName(menu.getAppName());
        m.setComponent(menu.getComponent());
        m.setName(menu.getName());
        m.setOrder(menu.getOrder());
        m.setPermissions(menu.getPermissions());
        m.setType(menu.getType());
        m.setPath(menu.getPath());
        m.setModifyDate(new Date());
        menuMapper.updateByPrimaryKey(m);
    }

    @Override
    public void delete(Long id) {
        menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Menu get(Long id) {
        return menuMapper.selectByPrimaryKey(id);
    }
}
